/**
 * 封装ajax、ajaxSubmit；
 * 处理返回的json；
 * {"statusCode":"300", "message":"操作失败"}
 * {"statusCode":"301", "message":"会话超时"}
 * 其他则执行回调函数。
 */

;(function($){
	$.vkoAjax = function(options) {
		options = $.extend(options, {
    		cache: false
    	});
    	var type = typeof options.type != "undefined" ? options.type : "get";
    	var data = typeof options.data != "undefined" ? options.data : "{}";
    	
    	$.ajax({
    		url: options.url,
    		type: type,
    		data: data,
    		beforeSend:VKO.ShowLoading,
    		cache: options.cache,
    		success: function(json){
    			json=VKO.strToJson(json);
    			var status=VKO.ajaxSuccess(json);
    			if(status=="200"){
    				if(typeof options.success != "undefined"){
    					options.success.call(this, json);
    				}
    			}else if(status=="300"){
    				if(typeof options.error != "undefined"){
    					options.error.call(this, json);
    				}else{
    					$.prompt({content:json.message,type:'error'});
    				}
    			}else if(status=="301"){
    				VKO.loadLogin();
    			}
    		},
    		complete: function(XMLHttpRequest, textStatus){
    			VKO.HideLoading();
    		},
    		error: VKO.ajaxError
    	});
	};
	$.vkoAjaxP = function(options) {
		options = $.extend(options, {
    		cache: false
    	});
    	var type = typeof options.type != "undefined" ? options.type : "get";
    	var data = typeof options.data != "undefined" ? options.data : "{}";
    	
    	$.ajax({
    		dataType:"jsonp",
    		url: options.url,
    		type: type,
    		data: data,
    		jsonp:"callback",
    		beforeSend:VKO.ShowLoading,
    		cache: options.cache,
    		success: function(json){
    			json=VKO.strToJson(json);
    			var status=VKO.ajaxSuccess(json);
    			if(status=="200"){
    				if(typeof options.success != "undefined"){
    					options.success.call(this, json);
    				}
    			}else if(status=="300"){
    				if(typeof options.error != "undefined"){
    					options.error.call(this, json);
    				}else{
    					$.prompt({content:json.message,type:'error'});
    				}
    			}else if(status=="301"){
    				VKO.loadLogin();
    			}
    		},
    		complete: function(XMLHttpRequest, textStatus){
    			VKO.HideLoading();
    		},
    		error: VKO.ajaxError
    	});
	};
	$.fn.vkoAjaxSubmit = function(form) {
		form = $.extend(form, {
    		cache: false
    	});
		
		this.ajaxSubmit({
			type: form.method || 'POST',
			url: $(form).attr("action"),
			cache: form.cache,
			beforeSend:VKO.ShowLoading,
    		success: function(json){
    			json=VKO.strToJson(json);
    			var status=VKO.ajaxSuccess(json);
    			if(status=="200"){
    				if(typeof form.success != "undefined"){
    					form.success.call(this, json);
    				}
    			}else if(status=="300"){
    				if(typeof form.error != "undefined"){
    					form.error.call(this, json);
    				}else{
    					$.prompt({content:json.message,type:'error'});
    				}
    			}else if(status=="301"){
    				VKO.loadLogin();
    			}
    		},
    		complete: function(XMLHttpRequest, textStatus){
    			VKO.HideLoading();
    		},
    		error: VKO.ajaxError
		});    	
	};
	$.fn.vkoAjaxSubmitP = function(form) {
		form = $.extend(form, {
    		cache: false
    	});
		
		this.ajaxSubmit({
			dataType:"jsonp",
			type: form.method || 'POST',
			url: $(form).attr("action"),
			cache: form.cache,
			jsonp:"callback",
			beforeSend:VKO.ShowLoading,
    		success: function(json){
    			json=VKO.strToJson(json);
    			var status=VKO.ajaxSuccess(json);
    			if(status=="200"){
    				if(typeof form.success != "undefined"){
    					form.success.call(this, json);
    				}
    			}else if(status=="300"){
    				if(typeof form.error != "undefined"){
    					form.error.call(this, json);
    				}else{
    					$.prompt({content:json.message,type:'error'});
    				}
    			}else if(status=="301"){
    				VKO.loadLogin();
    			}
    		},
    		complete: function(XMLHttpRequest, textStatus){
    			VKO.HideLoading();
    		},
    		error: VKO.ajaxError
		});    	
	};
})(jQuery);